<!DOCTYPE HTML>

<html>
<head>
<title>Quizhandler</title>
<link rel="stylesheet" href="styles/styles.css" type="text/css" />
</head>
<body>
<?php 
require_once'config.php';
require_once'functions.php';

	if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
	$num = $_POST['num'];
	$currentQuestion = $num;
//	$selectedAnswer = $_POST['questionOptions']; 
	$_SESSION['score'] = $_POST['score']; 
	$score = $_SESSION['score'] ;
	$studentID = $_SESSION['studentID'] ;
	}

$getQuestion = getQuestion($currentQuestion);
$num = $_POST['num'];
$checkAnswer = checkAnswer($currentQuestion);
$attempt =	 $_SESSION['attempt'] ;	
////////////////////////////////////////////////////////////////////////////////// GRADE ANSWER	
	$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME) or die ("Could not execute query");
	$query = "SELECT * FROM studentresponse JOIN question ON studentresponse.questionID=question.questionID WHERE studentID ='$studentID' AND testID='$testID'";
	$result = mysqli_query($con,$query) or die ("Could not execute getQuestion query");
if (isset($_POST['questionOptions'])){
 $selectedAnswer = $_POST['questionOptions'];
$checkAnswer = checkAnswer($currentQuestion);
$correctAnswer = $checkAnswer[0];
if ($selectedAnswer == $correctAnswer)

{	$score = ($_POST['score'] + 1);
	$feedbackStatus = "<span class='green'> correct.</span><br/><br/><img src='img/checkmark.png' width='64' height='64'>";
}
else
{
	$score = ($_POST['score']);
	$feedbackStatus = "<span class='red'> incorrect.</span><br/><br/><img src='img/delete.png' width='64' height='64'>";
}
	if (is_array($selectedAnswer))
{
	foreach($selectedAnswer as $answer)
	{
	$arrayAnswer = implode(",", $selectedAnswer);
	asort($selectedAnswer);
	}	
 }
	 if (strlen($arrayAnswer) == strlen($correctAnswer))
	 {
	if (strpos($arrayAnswer, $correctAnswer) !== false)
 {
	$arrayFeedbackStatus = "<span class='green'> correct.</span><br/><br/><img src='img/checkmark.png' width='64' height='64'>";
  	$score++;
}
 }
else
{
	$arrayFeedbackStatus = "<span class='red'> incorrect.</span><br/><br/><img src='img/delete.png' width='64' height='64'>";
}
}
$startTest = startTest($studentID, $currentQuestion, $testID);
$putStudentResponse = putStudentResponse($studentID, $attempt, $currentQuestion, $testID);
?><div class="overlay"></div>
<div class="alert">
  <?php if (!isset($_POST['questionOptions'])) { 
  echo "<img src='img/block.png' alt='' width='64' height='64'><br/><p>You have not selected an answer.</p>"; ?>
  <form method='post' action="postquiz.php" id="feedback">
    <input type="hidden" name="num" value="<?php echo $num ?>">
    <input type="hidden" name="score" value="<?php echo  $score; ?>">
    <input type="hidden" name="getNext" value="TRUE">
    <input type='submit' id='submit' name='back' value='Go Back' class='btn'>
  </form>
  <?php  }  ?>
  <?php if (isset($_POST['questionOptions'])) 
  {  ?>
  <p>You answered:
    <?php  if (is_array($selectedAnswer)) 

{echo $arrayAnswer; } 
else { ?>
    <?php echo $selectedAnswer;?>
    <?php } ?>
  </p>
  <p>The correct answer was <?php echo $correctAnswer ;?>. 
    You were
    <?php  if (is_array($selectedAnswer)) {
		 echo $arrayFeedbackStatus; 
		 } else { ?>
    <?php echo $feedbackStatus; ?> </p>
  <?php  } ?>
  <?php if ($num < 10) { ?>
  <form method="post" action="postquiz.php">
    <input type="hidden" name="attempt" value="<?php echo $attempt ?>">
    <input type="hidden" name="num" value="<?php echo $num + 1 ?>">
    <input type="hidden" name="score" value="<?php echo $score; ?>">
    <input type="submit" id="submit" name="next" value="Next Question" class="btn">
  </form>
  <?php } ?>
  <?php if ($num > 9) { ?>
  <form method="post" id="finished" action="results.php">
    <input type="hidden" name="attempt" value="<?php echo $attempt ?>">
    <input type="hidden" name="num" value="<?php echo $num + 1?>">
    <input type="hidden" name="score" value="<?php echo $score; ?>">
    <input type="submit" id="getResults" name="getResults" value="Get Results" class="btn">
  </form>
  <?php }} ?>
 </div>
</body>
</html>